<template>
	<v-layout type="main">
		<div class="alike-lis-header">
			<div class="lis-header-left">
				<div class="add-btn" @click="$router.push({ path: '/notebook/detail' })">+ 新增</div>

				<div class="btns-switch">
					<div @click="isCard = true">
						<img src="@/assets/img/List_card.png" v-if="isCard" />
						<img src="@/assets/img/list_nocard.png" v-else />
					</div>
					<div @click="isCard = false">
						<img src="@/assets/img/list_nolist.png" v-if="isCard" />
						<img src="@/assets/img/list_list.png" v-else />
					</div>
				</div>

				<div class="search-box">
					<el-input
						prefix-icon="el-icon-search"
						v-model="Pagedata.name"
						class="search"
						placeholder=""
						@input="searchChange"
					/>
				</div>
			</div>
		</div>

		<div class="table-layout" v-if="!isCard">
			<el-table :data="tableData" class="myself-table">
				<el-table-column prop="name" label="名称">
					<template slot-scope="{ row }">
						<el-popover placement="right-start" width="320" trigger="hover">
							<row-data :row="row" />
							<el-button
								type="text"
								@click="$router.push({ path: '/notebook/code', query: { id: row.id } })"
								slot="reference"
								>{{ row.name }}
							</el-button>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="状态">
					<template slot-scope="{ row }">
						<span :class="['alick-list-status', row.status]">●{{ statusMap[row.status] }}</span>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="name" label="资源配置" /> -->
				<el-table-column prop="update_time" label="更新时间" />
				<el-table-column label="运行时长">
					<template slot-scope="{ row }">
						{{ countRanTime(row.resource && row.resource.start_time) }}
					</template>
				</el-table-column>
				<el-table-column label="操作">
					<template slot-scope="{ row }">
						<!--<el-button type="text" @click="put(row)">修改</el-button>-->
						<!-- <el-button type="text" @click="del(row)">删除</el-button> -->
						<el-popover placement="top" width="80" trigger="click" v-model="row.visible">
							<p>您确定要删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="row.visible = false">取消</el-button>
								<el-button type="primary" size="mini" @click="del(row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference">删除</el-button>
							<!-- <el-button slot="reference" type="danger" size="mini">删除</el-button> -->
							<!-- <i slot="reference" class="el-icon-delete delete-btn" /> -->
						</el-popover>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="alike-lis-both" v-else>
			<ul class="alike-lis">
				<li v-for="(item, index) in tableData" :key="index">
					<div class="alike-lis-box">
						<div class="alike-lis-pic" @click="$router.push({ path: '/notebook/code', query: { id: item.id } })">
							<v-image :src="item.avatar ? item.avatar : require('@/assets/img/drone3.jpg')" alt="" />
						</div>

						<div class="alike-lis-word">
							<div class="alike-lis-title">
								<div class="name" @click="$router.push({ path: '/notebook/code', query: { id: item.id } })">
									{{ item.name }}
								</div>

								<template v-if="typeof item.status === 'string'">
									<span class="status-tag tag danger" v-if="['terminated', 'failed', 'Failed'].includes(item.status)">
										{{ statusMap[item.status] }}
									</span>
									<span
										class="status-tag tag success"
										v-else-if="['completed', 'Succeeded', 'running', 'Running'].includes(item.status)"
									>
										{{ statusMap[item.status] }}
									</span>
									<span class="status-tag tag warning" v-else>
										{{ statusMap[item.status] }}
									</span>
								</template>

								<el-popover placement="top" width="80" trigger="click" v-model="item.visible">
									<p>您确定要删除吗？</p>
									<div style="text-align: right; margin: 0">
										<el-button size="mini" type="text" @click="item.visible = false">取消</el-button>
										<el-button type="primary" size="mini" @click="del(item)">确定</el-button>
									</div>
									<i slot="reference" class="el-icon-delete delete-btn" />
								</el-popover>
							</div>

							<p class="alike-lis-hint">{{ item.desc }}</p>

							<div class="alike-lis-user">
								<span class="user-name">创建人：{{ item.create_user.nick_name }}</span>
								<span class="date">{{ item.create_time }}</span>
							</div>
						</div>
					</div>
				</li>
			</ul>
		</div>

		<div class="pagination-box" v-if="Pagedata.total > Pagedata.pageSize">
			<el-pagination
				background
				class="pagination"
				layout="total, prev, pager, next, jumper"
				:page-size="Pagedata.pageSize"
				:current-page.sync="Pagedata.pageNum"
				@current-change="handleCurrentChange"
				:total="Pagedata.total"
			>
			</el-pagination>
		</div>
	</v-layout>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { getExperiment, delExperiment } from "@/api/notebook";
import { paramMap } from "@/utils/maps";

export default {
	data() {
		return {
			notebook_flag: paramMap["notebook_flag"],
			showThisActive: 1,
			isCard: true,
			tableData: [],
			Pagedata: {
				pageNum: 1,
				pageSize: 12,
				total: 0,
				name: "",
			},
		};
	},
	async created() {
		this.Pagedata = { ...this.Pagedata, pageNum: this.pageNum, name: this.searchValue };
		this.getData();
		this.$EventBus.$on("experiment", (e) => {
			console.log(e);
			this.getData();
		});
	},
	computed: mapState({
		pageNum: (state) => state.menuStore.pageData.pageNum,
		searchValue: (state) => state.menuStore.searchValue,
	}),
	methods: {
		...mapActions(["updatePageNum", "updateSearch"]),
		async getData() {
			let res = await getExperiment({ ...this.Pagedata });
			this.tableData = res.data.list;
			this.Pagedata.total = res.data.total;
		},
		searchChange() {
			this.updatePageNum(1);
			this.Pagedata.pageNum = 1;
			this.updateSearch(this.Pagedata.name);
			this.getData();
		},
		put({ id }) {
			this.$router.push({
				path: "/notebook/detail",
				query: { id },
			});
		},
		async del(row) {
			// let res = await this.$isDel();
			// if (res) {
			await delExperiment(row);
			this.getData();
			// }
		},
		handleCurrentChange(val) {
			this.Pagedata.pageNum = val;
			this.updatePageNum(val);
			this.getData();
		},
		setListOrGraph() {
			this.list_flag = !this.list_flag;
		},
	},
	destroyed() {
		this.$EventBus.$off("experiment");
	},
};
</script>
<style lang="less" scoped>
.table-layout {
	margin-top: 20px;
}

.dimg {
	object-fit: cover;
}

.switch {
	display: flex;
	justify-content: space-between;

	// overflow: hidden;
	// padding-right: 500px;
	span {
		padding-right: 20px;
	}

	a {
		float: left;
		width: 20px;
		height: 20px;
		margin-left: 14px;
		margin-top: 10px;
		background-color: #767676;
	}

	.btn-pic {
		mask: url(~@/assets/img/file-icon/btn-pic.svg);
		mask-size: 20px 20px;
	}

	.btn-lis {
		mask: url(~@/assets/img/file-icon/btn-lis.svg);
		mask-size: 20px 20px;
	}

	.acticv {
		background-color: #409eff;
	}

	.add-btn {
		float: left;
	}
}
</style>
